package cn.com.generaldata.os.service.indicator_evaluation.prosperity_analysis;

import org.apache.commons.lang3.Validate;

import cn.com.generaldata.jsme_indicator_system.entity.IndicatorEvaluation;
import cn.com.generaldata.jsme_indicator_system.util.Constants;
import cn.com.generaldata.os.service.indicator_evaluation.IndicatorEvaluationCalculateService;
import cn.com.generaldata.os.vo.IndicatorAccountVO;

/**
 * 贷款余额指标增长率评估指标计算类.
 */
public class LoanBalanceGrowthRateCalculateService extends IndicatorEvaluationCalculateService {

	private String loanBalanceIndicatorName = "贷款余额";

	private String CPIIndicatorName = "CPI";

	@Override
	protected Double calculate(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		Double result = null;
		if (indicator.getSection() == Constants.INDICATOR_LEVEL_PROVINCE) {
			result = calculateProvinceResult(indicator, indicatorAccountVO);
		} else if (indicator.getSection() == Constants.INDICATOR_LEVEL_CITY) {
			// 市指标没确定先保留
		}
		return result;
	}

	/**
	 * 计算江苏省贷款余额指标增长率评估指标计算类.
	 * 
	 * @param indicator
	 * @param indicatorAccountVO
	 * @return
	 */
	private Double calculateProvinceResult(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		Double loanBalance = getLoanBalance(indicatorAccountVO, indicatorAccountVO.getYear());
		Double cpi = getCPI(indicatorAccountVO);

		Double loanBalanceCp = loanBalance * 100 / cpi;

		Double lastLoanBalance = getLoanBalance(indicatorAccountVO, indicatorAccountVO.getYear() - 1);

		return loanBalanceCp / lastLoanBalance - 1;

	}

	private Double getCPI(IndicatorAccountVO indicatorAccountVO) {

		Double cpi = getInputOrAccountingIndicatorValue(indicatorAccountVO, indicatorAccountVO.getYear(),
				CPIIndicatorName);

		Validate.isTrue(cpi != null);

		return cpi;
	}

	private Double getLoanBalance(IndicatorAccountVO indicatorAccountVO, int year) {

		Double loanBalance = getInputOrAccountingIndicatorValue(indicatorAccountVO, year, loanBalanceIndicatorName);

		Validate.isTrue(loanBalance != null);

		return loanBalance;
	}

}
